//
//  SEGLoginViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/13.
//

#import "SEGLoginViewModel.h"


@interface SEGLoginViewModel ()


@end

@implementation SEGLoginViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createLoginCommand];
    }
    return self;
}
- (void)seg_createLoginCommand {
    self.loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(RACTuple *tuple) {
        RACTupleUnpack(NSString *tel, NSString *password, NSString *imgCode, NSString *randomToken) = tuple;
        // 5.2.0改动：再未登录成功之前不保存账号密码到本地，登录成功再储存
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [[SEGLoginV2Manager sharedManager] requestLoginByTel:tel password:password imgCode:imgCode randomToken:randomToken loginType:@"" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    // 现在处于登录状态
                    [SEGLoginV2Manager seg_saveSignOutStatus:NO];
                    
                    [subscriber sendNext:@""];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}

@end
